﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_Project")]
    public class P_Project : IEntity<P_Project>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string BorrowerName { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ProjectTitle { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string ProjectCode { get; set; }
        public int ProductID { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string ProductName { get; set; }
        public int EnterpriseID { get; set; }
        [Column(TypeName = "nvarchar")]
        public string ProjectDescription { get; set; }
        public decimal Financing { get; set; }
        public decimal MinInvestment { get; set; }
        public decimal MaxInvestment { get; set; }
        public decimal IncreaseMoney { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime EndDate { get; set; }
        public int CycleAmount { get; set; }
        public DateTime ReleaseTime { get; set; }
        public DateTime FullScaleTime { get; set; }
        public DateTime LendingTime { get; set; }
        public DateTime BorrowInterestDate { get; set; }
        public int WaitDay { get; set; }
        public int GuaranteeCompanyID { get; set; }
        [Column(TypeName = "nvarchar")]
        public string CollateralOpinion { get; set; }
        [Column(TypeName = "nvarchar")]
        public string RiskCotrolSummary { get; set; }
        public int Status { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }
        public DateTime CreateTime { get; set; }
        public decimal OriginalRate { get; set; }
        public decimal GuaranteeRate { get; set; }

        public override P_Project Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_Project Find(System.Linq.Expressions.Expression<Func<P_Project, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Project.FirstOrDefault(where);
                if (item != null)
                {
                    this.BeginDate = item.BeginDate;
                    this.BorrowerName = item.BorrowerName;
                    this.CollateralOpinion = item.CollateralOpinion;
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    this.CycleAmount = item.CycleAmount;
                    this.EnterpriseID = item.EnterpriseID;
                    this.EndDate = item.EndDate;
                    this.Financing = item.Financing;
                    this.GuaranteeCompanyID = item.GuaranteeCompanyID;
                    this.ID = item.ID;
                    this.IncreaseMoney = item.IncreaseMoney;
                    this.MaxInvestment = item.MaxInvestment;
                    this.MinInvestment = item.MinInvestment;
                    this.ProductID = item.ProductID;
                    this.ProductName = item.ProductName;
                    this.ProjectCode = item.ProjectCode;
                    this.ProjectDescription = item.ProjectDescription;
                    this.ProjectTitle = item.ProjectTitle;
                    this.ReleaseTime = item.ReleaseTime;
                    this.FullScaleTime = item.FullScaleTime;
                    this.LendingTime = item.LendingTime;
                    this.BorrowInterestDate = item.BorrowInterestDate;
                    this.RiskCotrolSummary = item.RiskCotrolSummary;
                    this.Status = item.Status;
                    this.WaitDay = item.WaitDay;
                    this.OriginalRate = item.OriginalRate;
                    this.GuaranteeRate = item.GuaranteeRate;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_Project, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Project.Any(where);
            }
        }
    }
}